<!--
 * @Description: 岗位树选择(单选,三级拼接显示,获取岗位code，岗位id，岗位名称)
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-11-23 10:03:59
 * @LastEditTime: 2022-12-19 15:33:56
 * @FilePath: \cscec-pms-admin\src\components\Select\SelectJobTree.vue
-->
<template>
  <div>
    <a-tree-select
      v-model="currentValue"
      v-bind="$attrs"
      :tree-data="tree"
      :replace-fields="replaceFields"
      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
      :load-data="onLoadJob"
      :tree-node-label-prop="'label'"
      labelInValue
      class="width-100"
      @change="handleChange"
    >
      <a-icon
        v-if="leaderLoading"
        slot="suffixIcon"
        type="loading"
      />
    </a-tree-select>
  </div>
</template>
<script>
import { getCustOrgTree, getJobs } from '@/api/subassembly/TreeSelectOrganization'
export default {
  name: 'SelectJobTree',
  props: {
    value:{
      type: Object,
      default:()=>{}
    }
  },
  data() {
    return {
      // 树
      tree: [],
      // 字段
      replaceFields: {
        title: 'title',
        value: 'id',
        key: 'id',
        children: 'children'
      },
      // 加载
      leaderLoading: false
    }
  },
  created() {
    this.getTree()
  },
  computed:{
    // 转换绑定数据
    currentValue: {
      get: function (v) {
        const { noticePostName, noticePostId } = this.$clone(this.value)
        return {
          label:noticePostName,
          value:noticePostId
        }
      },
      set: function (value) {
        this.$emit('input', value)
      },
    },
  },
  methods: {
    /**
     * @description: 过滤树
     * @return {void}
     */
    formatTree(tree, label = '') {
      const list = []
      for (let i = 0; i < tree.length; i++) {
        if (tree[i]?.children && tree[i].children.length > 0) {
          list.push({
            ...tree[i],
            title: tree[i].label,
            label: `${label}-${tree[i].label}`,
            disabled: true,
            children: this.formatTree(tree[i].children, tree[i].label)
          })
        } else {
          list.push({
            ...tree[i],
            title: tree[i].label,
            label: `${label}-${tree[i].label}`,
            disabled: true
          })
        }
      }
      return list
    },
    /**
     * @description: 获取组织树
     * @return {void}
     */
    async getTree() {
      const res = await getCustOrgTree({
        baseOrgType: 1,
        orgDeptTagCodeAll: 1,
        enable: 1,
        containVirtual: null
      })
      if (res.status == this.SUCCESS_CODE) {
        this.tree = this.formatTree(res.data)
      }
    },
    /**
     * @description: 树异步加载岗位
     * @param {object} dataRef 节点对象数据
     * @return {promise}
     */
    onLoadJob(node) {
      const { dataRef } = node
      return new Promise((resolve) => {
        if (dataRef?.data?.type == 2 || dataRef?.data?.type == 7) {
          getJobs({
            orgId: dataRef.id,
            resultWithEmployee: false
          }).then((res) => {
            if (this.leaderLoading) this.leaderLoading = false
            if (res.status == 200) {
              dataRef.children = res.data.map((item) => ({
                data: {
                  ...item,
                  parentId: item.orgId
                },
                id: item.id,
                title: item.name,
                pid: item.pid,
                type: item.type,
                label: `${dataRef.label}-${item.name}`,
                isLeaf: true
              }))
              this.$forceUpdate()
            }
            resolve()
          })
        } else {
          resolve()
        }
      })
    },
    /**
     * @description: 选中节点
     * @return {void}
     */    
    handleChange(value, label, extra){
      const data = extra.triggerNode.dataRef
      this.currentValue = {
        noticePostCode: data.data.code,
        noticePostName: data.label,
        noticePostId: data.id
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
